import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login'
import Home from '@/views/Home'
import Welcome from '../components/Welcome.vue'
import Users from '../components/users/Users.vue'
import Rights from '../components/prower/Rights.vue'
import Roles from '../components/prower/Roles.vue'
import Order from '../components/orders/Order.vue'
import Report from '../components/report/Report.vue'


Vue.use(VueRouter);

const routes = [
    {path: '*', redirect: '/login'},   //问题:  / 和 * 区别(应用场景有何不同)?
    {path: '/login', component: Login},
    {
        path: '/home',
        component: Home,
        redirect: '/welcome',
        children: [
            {
                path: '/welcome',
                component: Welcome,
            },
            {
                path: '/users',
                component: Users,
            },
            {
                path: '/rights',
                component: Rights,
            },
            {
                path: '/roles',
                component: Roles,
            },
            {
                path: '/orders',
                component: Order,
            },
            {
                path: '/reports',
                component: Report,
            }
        ]
    }
];

const router = new VueRouter({
    routes
});

//路由守卫
//to,去哪 from,从来来 next是否进入
router.beforeEach((to, from, next) => {
    let token = window.sessionStorage.getItem("token");
    if (to.path == "/login") token ? next("/home") : next();
    if (!token) next("/login");
    next()
});

export default router
